package com.kpx.app.view;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kpx.app.R;
import com.kpx.app.activity.ActivityUserinfoSign;
import com.kpx.app.bean.TagBean;
import com.kpx.app.http.HttpDataApi;
import com.kpx.app.http.HttpDataLoader;
import com.kpx.app.http.HttpDataReqUrl;
import com.kpx.app.http.HttpDataRequest;
import com.kpx.app.http.ResObj;
import com.kpx.app.utils.MyHashMap;
import com.kpx.app.utils.uiutils.ToastUtil;
import com.kpx.app.wigdet.FlowLayout;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/4/11.
 */

public class ViewSign extends LinearLayout {
    private final Context context;
    @BindView(R.id.txt_sign)
    TextView txtSign;
    @BindView(R.id.flow_list)
    FlowLayout flowList;
    @BindView(R.id.flow_select)
    FlowLayout flow_select;
    @BindView(R.id.txt_input)
    TextView txtInput;
    private TagBean tagBean;

    public ViewSign(Context context) {
        super(context);
        this.context = context;
        initView(context);
    }

    private void initView(Context context) {
        View view = View.inflate(context, R.layout.view_sign, this);
        ButterKnife.bind(view);
        txtInput.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                showSureDialog();
            }
        });
    }

    public void setData(TagBean tagBean) {
        this.tagBean = tagBean;
        txtSign.setText(tagBean.getTagName());
        final ArrayList<TagBean.ChildTagsBean> childs = (ArrayList<TagBean.ChildTagsBean>) tagBean.getChildTags();
        if (childs != null) {
            for (int i = 0; i < childs.size(); i++) {
                final TagView t = new TagView(context);
                t.setData(childs.get(i));
                t.setTag(childs.get(i).getDmId());
                t.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (((TagView) t).back().isSelect()) {
                            remove(t);
                        } else {
                            if(flow_select.getChildCount()>=3){
                                ToastUtil.getInstance().showToast(context,"每种类型标签只能选择3种");
                                return;
                            }
                            add(t);
                        }
                    }
                });
                flowList.addView(t);
                ViewGroup.LayoutParams p = (ViewGroup.LayoutParams) t.getLayoutParams();
                p.width = LayoutParams.WRAP_CONTENT;
                p.height = LayoutParams.WRAP_CONTENT;
                t.setLayoutParams(p);
            }
        }

    }

    private void add(TagView t) {
        t.back().setSelect(true);
        t.refresh();
        final TagView f = new TagView(context);
        f.setTag(t.back().getDmId());
        f.setData(t.back());
        f.setTopon();
        f.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                for (int i = 0; i < flowList.getChildCount(); i++) {
                    if (((Long) flowList.getChildAt(i).getTag()).longValue() == ((Long) f.getTag()).longValue()) {
                        ((TagView) flowList.getChildAt(i)).back().setSelect(false);
                        ((TagView) flowList.getChildAt(i)).refresh();
                    }
                }
                for (int i = 0; i < flow_select.getChildCount(); i++) {
                    if (((Long) flow_select.getChildAt(i).getTag()).longValue() == ((Long) f.getTag()).longValue()) {
                        flow_select.removeViewAt(i);
                    }
                }
            }
        });
        flow_select.addView(f);
        ViewGroup.LayoutParams p = (ViewGroup.LayoutParams) f.getLayoutParams();
        p.width = LayoutParams.WRAP_CONTENT;
        p.height = LayoutParams.WRAP_CONTENT;
        f.setLayoutParams(p);
    }

    private void remove(View v) {
        ((TagView) v).back().setSelect(false);
        ((TagView) v).refresh();
        for (int i = 0; i < flow_select.getChildCount(); i++) {
            if (((Long) flow_select.getChildAt(i).getTag()).longValue() == ((Long) v.getTag()).longValue()) {
                flow_select.removeViewAt(i);
            }
        }
    }

    private void showSureDialog() {
        final Dialog dialog = new Dialog(context);
        View view = View.inflate(context, R.layout.dialog_show, null);
        final EditText et = (EditText) view.findViewById(R.id.et);
        view.findViewById(R.id.txt_sure).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(et.getText().toString().trim())) {
                    ToastUtil.getInstance().showToast(context, "请输入标签名称");
                    return;
                }
                dialog.dismiss();
                MyHashMap map2 = new MyHashMap();
                map2.put("parentId", tagBean.getDmId() + "");//parentId(大标签ID),tagName(名称)
                map2.put("tagName", et.getText().toString().trim());//parentId(大标签ID),tagName(名称)
                new HttpDataLoader(new HttpDataApi.OnRequestCallback() {
                    @Override
                    public void onRequestSuccess(String url, ResObj result, boolean isFrmCache) {
                        if (result.getCode() == 0) {
                            if (result.getData() != null) {
                                TagBean.ChildTagsBean b = (TagBean.ChildTagsBean) result.getData();
                                if (b != null) {
                                    tagBean.getChildTags().add(b);
                                    flowList.removeAllViews();
                                    setData(tagBean);
                                }
                            }
                        } else {
                            ToastUtil.getInstance().showToast(context, result.getMsg());
                        }
                    }

                    @Override
                    public void onRequestError(String url, int statusCode, String error) {
                        ToastUtil.getInstance().showToast(context, error);
                    }

                }, TagBean.ChildTagsBean.class, HttpDataRequest.TYPE_OBJECT).POST(HttpDataReqUrl.URL_POST_GET_TAG_ADD, true, map2);
            }
        });
        dialog.setContentView(view);
        dialog.setCancelable(false);
        dialog.setCanceledOnTouchOutside(true);
        dialog.show();
    }
}
